package drds.binlog.client;

import drds.binlog.client.impl.ClusterConnector;
import drds.binlog.client.impl.ClusterNodeAccessStrategy;
import drds.binlog.client.impl.SimpleConnector;
import drds.binlog.client.impl.SimpleNodeAccessStrategy;
import drds.binlog.common.zookeeper.ZooKeeperClient;

import java.net.SocketAddress;
import java.util.List;

/**
 * canal connectors创建工具类
 */
public class Connectors
{

    /**
     * 创建单链接的客户端链接
     */
    public static Connector newSingleConnector(SocketAddress address, String destination, String username,
                                               String password)
    {
        SimpleConnector simpleConnector = new SimpleConnector(address, username, password, destination);
        simpleConnector.setSoTimeout(60 * 1000);
        simpleConnector.setIdleTimeout(60 * 60 * 1000);
        return simpleConnector;
    }

    /**
     * 创建带cluster模式的客户端链接，自动完成failover切换
     */
    public static Connector newClusterConnector(List<? extends SocketAddress> addresses, String destination,
                                                String username, String password)
    {
        ClusterConnector clusterConnector = new ClusterConnector(username,
                password,
                destination,
                new SimpleNodeAccessStrategy(addresses));
        clusterConnector.setSoTimeout(60 * 1000);
        clusterConnector.setIdleTimeout(60 * 60 * 1000);
        return clusterConnector;
    }

    /**
     * 创建带cluster模式的客户端链接，自动完成failover切换，服务器列表自动扫描
     */
    public static Connector newClusterConnector(String zkServers, String destination, String username,
                                                String password)
    {
        ClusterConnector clusterConnector = new ClusterConnector(username,
                password,
                destination,
                new ClusterNodeAccessStrategy(destination, ZooKeeperClient.getZkClient(zkServers)));
        clusterConnector.setSoTimeout(60 * 1000);
        clusterConnector.setIdleTimeout(60 * 60 * 1000);
        return clusterConnector;
    }
}
